<script setup lang="js">
import UserStepsPlanAPI from "@/api/stepsPlan/user-steps-plan";
import UserEnergyPlanAPI from "@/api/energyPlan/user-energy-plan";
import UserWeekPlanAPI from "@/api/weekPlan/user-week-plan";
import UserAPI from "@/api/system/user";

const props = defineProps({
  increment: Number,
  title: String,
  field: {
    type:String,
    default:''
  },
  confirmText: {
    type: String,
    default: "确认目标！",
  },
});
const emit = defineEmits(["getPlan"]);
const temp = ref(0);
const isShow = ref(false);
const select = ref();
const submitPlan = () => {
  if (props.title === "每日步数") {
    UserStepsPlanAPI.add({ steps: temp.value }).then((res) => {
      emit("getPlan");
      handleClose();
      uni.showToast({ title: "添加成功", icon: "none" });
    });
  } else if (props.title === "能量限额") {
    UserEnergyPlanAPI.add({ energy: temp.value }).then((res) => {
      emit("getPlan");
      handleClose();
      uni.showToast({ title: "添加成功", icon: "none" });
    });
  } else if (props.title === "每周运动时常") {
    UserWeekPlanAPI.add({ sportTime: temp.value }).then((res) => {
      emit("getPlan");
      handleClose();
      uni.showToast({ title: "添加成功", icon: "none" });
    });
  } else {
    let userProfileForm={}
    userProfileForm[props.field]=temp.value
    UserAPI.updateProfile(userProfileForm).then((res) => {
      emit("getPlan");
      handleClose();
      uni.showToast({ title: "添加成功", icon: "none" });
    });
  }
};
const handleClose = () => {
  temp.value = 0
  isShow.value = false;
  emit("getPlan");
};
const handleShow = (data) => {
  select.value=Array.from({ length: 251 }, (_, i) => (i + 1) * props.increment)
  temp.value = data;
  isShow.value = true;
};
defineExpose({ handleShow });
</script>

<template>
  <wd-popup v-if="isShow" v-model="isShow" position="bottom">
    <view style="max-height: 50vh; height: 50vh">
      <view style="display: flex; align-items: center; justify-content: space-between">
        <view style="margin-left: 20px">{{ title }}</view>
        <wd-button type="icon" icon="close" @click="handleClose"></wd-button>
      </view>
      <wd-picker-view v-model="temp" :columns="select" />
      <view style="text-align: center">
        <wd-button @click="submitPlan">{{ confirmText }}</wd-button>
      </view>
    </view>
  </wd-popup>
</template>

<style scoped lang="scss"></style>
